<%@ page import="java.util.jar.Attributes.Name"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*,java.util.*" %>
    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Degree-discipline</title>
<link href="main.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
	function setMajorName(name) {
    document.forms["form1"].discipline_name.value = name;
	}
</script>
</head>
<body>
<%
	// Get location from HTTP request
	Applicant a = (Applicant) session.getAttribute("applicant");
	LinkedList<Degree> degrees = a.getDegrees();
	Degree degree = null;
	University univ = null;
	if(degrees == null)
	{
		degrees = new LinkedList<Degree>();
		a.setDegrees(degrees);
	}
	degree = degrees.getLast();
	univ = degree.getUniversity();
	String university_id = (String)request.getParameter("univ_id");
	String university = (String)request.getParameter("university");
	String university_name = (String)request.getParameter("university_name");
	DatabaseAccessObject dao = new DatabaseAccessObject();
	int univ_id;
	if (university_id != null && university == null){  
		univ_id = Integer.valueOf(university_id);
		// User choose a exist university
		if(univ_id != -1)
		{
//			university = dao.getUniversityNameById(univ_id);
			univ.setId(univ_id);
			univ.setName(university_name);
		}
		//User choose a university he entered for previous degree
		else
		{
			univ.setId(-1);
			univ.setName(university_name);
		}
	}
	else {	
		// User manually enter a university
		univ.setId(-1);
		univ.setName(university);
	}
%>
<h1>Provide degrees - Choose discipline</h1>
<%
	//majors from the database
	LinkedList<Major> disciplines = dao.getMajors();
	Major major  = null;
	/* find the universities the user has entered manually for this location
	and append them to the end of the major list
	*/
	Set<String> major_input_set = new HashSet<String>();
	String major_name = null;
	int id;
	for(int i = 0;i<degrees.size()-1;i++)
	{
		major = degrees.get(i).getMajor();
		major_name = major.getName();
		//if the major entered by the user is not in the database 
		//or the major list, append it to the end of the major list
		if(major.getId() == -1)
		{
			id = dao.getMajorIdByName(major_name);
			if((id == -1)&&!major_input_set.contains(major_name))
			{
				major_input_set.add(major_name);
				disciplines.add(major);
			}
		}
	}
%>
<form name="form1" action="degree_moredegree.jsp" method="post">
	<% for(int i=0;i<disciplines.size();i++){	%>
	<input type="radio" name="discipline_id" value="<%= disciplines.get(i).getId() %>" onclick = "setMajorName('<%= disciplines.get(i).getName() %>')"
	<%= (i==0) ? "checked=\"yes\"" : "" %>>  
	<%= disciplines.get(i).getName() %> <br />
	<% } %>
	<input type="hidden" name="discipline_name" value="">
	<h2>If not found, input your Major below:</h2>
	<input type="text" name="discipline" value=""><br/>
	<hr>
	<strong>Degree Awarded/Expect Date:</strongDegree><br />
	<strong>Month</strong> <input type="number" name="month" value="" min="1" max="12" size="3" maxlength="2">
	<strong>Year</strong> <input type="text" name="year" value="" size="5" maxlength="4"><br/>
	<br/>
	<strong>GPA</strong> <input type="text" name="gpa" value="" size="3">
	<strong>Title of degree</strong>
	<select name = "title">
		<option value="bs">BS</option>
		<option value="ms">MS</option>
		<option value="phd">Phd</option>
	<select><br/><br/>
	<input type="submit" value = "submit">
</form>	
</body>
</html>